package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwcaT;
import cn.e4j.modular.crm.entity.GwcbT;
import cn.e4j.modular.crm.entity.GwccT;
import cn.e4j.modular.crm.service.GwcaTService;
import cn.e4j.modular.erp.service.impl.T100ServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 销售合同
 * Created by miaoyl on 2019-1-11.
 */
@Controller
@RequestMapping("/crm/gwci001")
public class Gwci001Controller extends BaseController {
    @Autowired
    private GwcaTService gwcaTService;

    @Autowired
    private T100ServiceImpl t100Service;

    @Autowired
    private SysAuthorityService sysAuthorityService;

    /**
     * 跳转投标信息申请管理页面
     */
    @RequiresPermissions("crm:gwci001:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwci001.html";
    }

    /**
     * 查询竞争分析列表
     */
    @RequestMapping("/getGwcalist")
    @RequiresPermissions("crm:gwci001:init")
    @ResponseBody
    public ResponseData getGwcalist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }
        List<Map<String, Object>> result = InvokeUtil.invokeBeanMethod( "gwcaTMapper,getGwcaList", page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取新增默认值
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        addDataDto.put("GWCAORIG", shiroUser.getORG_ID());
        addDataDto.put("GWCAORIU", shiroUser.getID());
        addDataDto.put("GWCAORIU_NAME", shiroUser.getNAME());
        addDataDto.put("GWCAORIG_NAME", shiroUser.getSysOrgT().getNAME());
        addDataDto.put("GWCADOCNO", "XS"+ DateUtils.formatDate(new Date(),"yyyyMMddHHmmssSSS"));
        addDataDto.put("GWCADOCDATE", DateUtils.formatDate(new Date(), "yyyy-MM-dd"));
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwci001:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        try {
            List<GwcbT> gwcbTList = JSONObject.parseArray(jsonObject.getString("gwcbList"), GwcbT.class);
            List<GwccT> gwccTList = JSONObject.parseArray(jsonObject.getString("gwccList"), GwccT.class);
            GwcaT gwcaT = JSONObject.parseObject(jsonObject.getString("gwcaData"), GwcaT.class);
            gwcaTService.addData(gwcbTList, gwccTList, gwcaT);
        } catch (Exception e) {
            e.printStackTrace();
            Dto dto = Dtos.newDto();
            dto.setAppCode(500);
            dto.setAppMsg(e.getMessage());
            return ResponseData.success(dto);
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwci001:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        try {
            List<GwcbT> gwcbTList = JSONObject.parseArray(jsonObject.getString("gwcbList"), GwcbT.class);
            List<GwccT> gwccTList = JSONObject.parseArray(jsonObject.getString("gwccList"), GwccT.class);
            GwcaT gwcaT = JSONObject.parseObject(jsonObject.getString("gwcaData"), GwcaT.class);
            gwcaTService.editData(gwcbTList, gwccTList, gwcaT);
        }catch (Exception e) {
            e.printStackTrace();
            Dto dto = Dtos.newDto();
            dto.setAppCode(500);
            dto.setAppMsg(e.getMessage());
            return ResponseData.success(dto);
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwci001:del")
    @ResponseBody
    public ResponseData delete(GwcaT gwcaT) {
        gwcaTService.deleteData(gwcaT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwci001:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwcaT> gwcaTList = inDto.getReqList(GwcaT.class);
        gwcaTService.delDataBatch(gwcaTList);
        return SUCCESS_TIP;
    }

    /**
     * 根据订单编号查询GWCB、GWCC数据
     */
    @RequestMapping("/getTable")
    @ResponseBody
    public ResponseData getTable(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = new Dto();
        List<Map<String, Object>> gwcbList = this.gwcaTService.getGwcbData(inDto);
        List<Map<String, Object>> gwccList = this.gwcaTService.getGwccData(inDto);
        outDto.put("gwcbList",gwcbList);
        outDto.put("gwccList",gwccList);
        return ResponseData.success(outDto);
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwci001:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwcaT> gwcaTList = inDto.getReqList(GwcaT.class);
        Dto outDto = gwcaTService.updateStatus(gwcaTList);
        if(outDto.getAppCode() == 500) {
            return new ErrorResponseData(500,outDto.getAppMsg());
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取税种、税率、汇率类型、汇率及客户发货地址, 客户地址
     */
    @RequestMapping("/getGwakData")
    @ResponseBody
    public ResponseData getGwakData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = this.gwcaTService.getGwakData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询收款计划
     */
    @RequestMapping("/getGwccList")
    @RequiresPermissions("crm:gwci001:init")
    @ResponseBody
    public ResponseData getGwcclist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = this.gwcaTService.getGwccList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询合同明细
     */
    @RequestMapping("/getGwcbList")
    @RequiresPermissions("crm:gwci001:init")
    @ResponseBody
    public ResponseData getGwcblist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = this.gwcaTService.getGwcbList(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getBookmarkData")
    @ResponseBody
    public ResponseData getBookmarkData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page page = dtoWapper.getPage();
        List<Dto> gwgiTList = gwcaTService.getBookmarkData(page,inDto);
        return new PageReData(page, gwgiTList);
    }

    @RequestMapping("/transferERP")
    @ResponseBody
    public ResponseData transferERP(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        try {
            gwcaTService.createOrder(inDto.getString("GWCADOCNO"), inDto.getString("TYPE"));
        }catch (Exception e){
            e.printStackTrace();
            return new ErrorResponseData(500, e.getMessage());
        }
        return SUCCESS_TIP;
    }

//    /**
//     * 获取配置说明
//     */
//    @RequestMapping("/getImafuc007")
//    @ResponseBody
//    public ResponseData getImafuc007(@DtoParam DtoWapper dtoWapper) {
//        Dto inDto = dtoWapper.getDto();
//        List<Dto> result = this.t100Service.getImafuc007(inDto);
//        return ResponseData.success(result);
//    }
}
